package com.dongdong.service.impl;

import com.dongdong.mapper.TbContentCategoryMapper;
import com.dongdong.model.TbContentCategory;
import com.dongdong.model.TbContentCategoryExample;
import com.dongdong.service.ContentCategoryService;
import com.dongdong.vo.ContentCategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    @Override
    public List<ContentCategoryVO> getCategoryList() {
        List<TbContentCategory> catelist = contentCategoryMapper.selectByExample(null);
        List<ContentCategoryVO> collect = catelist.stream().map(catgory -> {
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(catgory, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ContentCategoryVO> FindName(ContentCategoryVO categoryVO) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        if (categoryVO.getName() == null) {
            return null;
        }
        example.createCriteria().andNameLike("%" + categoryVO.getName() + "%");
        List<TbContentCategory> contentcatelist = contentCategoryMapper.selectByExample(example);
        List<ContentCategoryVO> collect = contentcatelist.stream().map(catelist -> {
            ContentCategoryVO vo = new ContentCategoryVO();
            BeanUtils.copyProperties(catelist, vo);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public int InsertCate(ContentCategoryVO categoryVO) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(categoryVO, category);
        int insert = contentCategoryMapper.insert(category);
        return insert;
    }

    @Override
    public int DelCate(Long id) {
        int i = contentCategoryMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public ContentCategoryVO getById(Long id) {
        TbContentCategory tbcontencategory = contentCategoryMapper.selectByPrimaryKey(id);
        ContentCategoryVO vo = new ContentCategoryVO();
        BeanUtils.copyProperties(tbcontencategory,vo);
        return vo;
    }

    @Override
    public int Update(ContentCategoryVO categoryVO) {
        TbContentCategory category = new TbContentCategory();
        BeanUtils.copyProperties(categoryVO,category);
        int i = contentCategoryMapper.updateByPrimaryKeySelective(category);
        return i;
    }

    @Override
    public int deleteCate(Long[] arr) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        example.createCriteria().andIdIn(Arrays.asList(arr));
        int i = contentCategoryMapper.deleteByExample(example);
        return i;
    }
}
